package pdm.yamba;

import java.util.List;

import pdm.yamba.adapter.StatusTimelineAdapter;
import pdm.yamba.dto.Status;
import pdm.yamba.feed.TwitterFeed;
import pdm.yamba.listener.IStatusRefreshListener;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class TimelineActivity extends BaseActivity {
	
	StatusTimelineAdapter itemsAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_timeline);
		/*
		app.addStatusRefreshListener(new IStatusRefreshListener() {
			@Override
			public void onRefreshComplete(List<Status> newStatuses) 
			{
				// get the last statuses stored in memory
				//List<Status> statuses = ((MainApplication)getApplication()).getStatuses();
			    // call this each time a new Status is added to the list
			    //itemsAdapter.addAll(statuses);
			    //itemsAdapter.notifyDataSetChanged();
			}

			@Override
			public void onRefreshProgress(Status status) 
			{
				List<Status> statuses = ((MainApplication)getApplication()).getStatuses();
				if(statuses.size() < app.getTimelineLength())
					itemsAdapter.notifyDataSetChanged();
			}		
		});
		*/
/*		
		TwitterService twitterService = app.getTwitterService();		
		// listen for changes whenever a refresh occurs, to update the statuses stored in memory
		twitterService.addStatusRefreshListener(new IStatusRefreshListener() 
		{
			@Override
			public void onRefreshComplete(List<Status> newStatuses) 
			{
				// get the last statuses stored in memory
				//List<Status> statuses = ((MainApplication)getApplication()).getStatuses();
			    // call this each time a new Status is added to the list
			    //itemsAdapter.addAll(statuses);
			    //itemsAdapter.notifyDataSetChanged();
			}

			@Override
			public void onRefreshProgress(Status status) 
			{
				List<Status> statuses = ((MainApplication)getApplication()).getStatuses();
				if(statuses.size() < app.getTimelineLength())
					itemsAdapter.notifyDataSetChanged();
			}
        });
*/		
		// get the last statuses stored in memory
	    itemsAdapter = new StatusTimelineAdapter(this, R.layout.timeline_item, app.getStatuses());
	    
	    // Configure the list view
	    ListView statusesView = (ListView) findViewById(R.id.statusesView);
	    statusesView.setAdapter(itemsAdapter);
	    
	    // event handler for a Status selection. Redirects to StatusActivity.
	    statusesView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
	    {
	    	
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
	        {
	    		// Show full status (Detail activity) of the selected item
	            Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
	            // sending data
	            intent.putExtra("statusIndex",String.valueOf(position));
	            startActivity(intent);
	        }
	    });
		Log.w("Yamba", "TimeLineActivity onCreate");
	};
	

	protected void onResume() 
	{	
    	super.onResume();		
		//app.RefreshStatus();
    	
    	MainApplication application = (MainApplication) getApplication();
    	
    	if(application.getAutomaticTimelineUpdate())
	    {
    		updateList();
	    }

		Log.w("Yamba", "TimeLineActivity onResume");
	}
	
	
	@Override	
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		
		MenuItem itemTimeline = menu.findItem(R.id.action_timeline);
		itemTimeline.setVisible(false);	
		
		return true;
	}
	
	public void updateList()
	{
    	ListView statusesView = (ListView) findViewById(R.id.statusesView);
    	
    	MainApplication application = (MainApplication) getApplication();
    	
		itemsAdapter = new StatusTimelineAdapter(this, R.layout.timeline_item, application.getStatuses());
		statusesView.setAdapter(itemsAdapter);
		itemsAdapter.notifyDataSetChanged();		
	}
}
